package com.das.a2048help

import android.accessibilityservice.AccessibilityService
import android.accessibilityservice.GestureDescription
import android.annotation.SuppressLint
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.graphics.Path
import android.os.Build
import android.util.Log
import android.view.accessibility.AccessibilityEvent
import androidx.annotation.RequiresApi


class MyAccessibilityService : AccessibilityService() {

    companion object {
        const val SWIPE_UP = "com.das.a2048help.SWIPE_UP"
        const val SWIPE_DOWN = "com.das.a2048help.SWIPE_DOWN"
        const val SWIPE_LEFT = "com.das.a2048help.SWIPE_LEFT"
        const val SWIPE_RIGHT = "com.das.a2048help.SWIPE_RIGHT"

    }

    @SuppressLint("UnspecifiedRegisterReceiverFlag")
    override fun onServiceConnected() {
        super.onServiceConnected()
        Log.d("MyAccessibilityService", "service已经连接")
        val filter = IntentFilter()
        filter.addAction(SWIPE_UP)
        filter.addAction(SWIPE_DOWN)
        filter.addAction(SWIPE_LEFT)
        filter.addAction(SWIPE_RIGHT)
        registerReceiver(swipeReceiver, filter)
    }

    private val swipeReceiver = object : BroadcastReceiver() {
        @RequiresApi(Build.VERSION_CODES.O)
        override fun onReceive(context: Context, intent: Intent) {
            val action = intent.action
            if (action != null) {
                Log.d("MyAccessibilityService", "接收到广播了 $action");
                when (action) {
                    SWIPE_UP -> swipeUp()
                    SWIPE_DOWN -> swipeDown()
                    SWIPE_LEFT -> swipeLeft()
                    SWIPE_RIGHT -> swipeRight()
                }
            }
        }
    }

    override fun onAccessibilityEvent(event: AccessibilityEvent?) {}

    override fun onInterrupt() {}

    private fun performSwipe(path: Path) {

        val gestureDescription = GestureDescription.Builder()
            .addStroke(GestureDescription.StrokeDescription(path, 0, 500L))
            .build()

        dispatchGesture(gestureDescription, object : GestureResultCallback() {
            override fun onCompleted(gestureDescription: GestureDescription) {
                super.onCompleted(gestureDescription)
                Log.d("MyAccessibilityService", "Gesture completed")
                sendGestureBroadcast(true)
            }

            override fun onCancelled(gestureDescription: GestureDescription) {
                super.onCancelled(gestureDescription)
                Log.d("MyAccessibilityService", "Gesture cancelled")
                sendGestureBroadcast(false)
            }
        }, null)
    }

    private fun sendGestureBroadcast(success: Boolean) {
        val intent = Intent("com.example.GESTURE_RESULT")
        intent.putExtra("gesture_success", success)
        sendBroadcast(intent)
    }
    fun swipeUp() {
        val path = Path().apply {
            moveTo(500f, 1500f) // Starting point (x, y)
            lineTo(500f, 1000f)
        }
        performSwipe(path)
    }

    fun swipeDown() {

        val path = Path().apply {
            moveTo(500f, 1000f)
            lineTo(500f, 1500f)
        }
        performSwipe(path)
    }

    fun swipeLeft() {

        val path = Path().apply {
            moveTo(800f, 1300f)
            lineTo(300f, 1300f)
        }
        performSwipe(path)
    }

    fun swipeRight() {
        val path = Path().apply {
            moveTo(300f, 1300f)
            lineTo(800f, 1300f)
        }
        performSwipe(path)
    }

    override fun onDestroy() {
        super.onDestroy()
        unregisterReceiver(swipeReceiver)
    }
}